package users

import (
	"github.com/gin-gonic/gin"
	"go_auth/ext/app"
	"go_auth/ext/e"
	"go_auth/ext/jwt"
	"go_auth/ext/settings"
	"go_auth/servers/users"
	"go_auth/utils/commons"
	"go_auth/utils/redis"
	"net/http"
)

//TODO   所有输入参数校验
/**
用户登录
目前就用户名+密码+来源(后续可以扩展,比如登录的版本型号)
*/
func Login(c *gin.Context) {
	var appG = app.Gin{C: c}

	username := c.PostForm("username")
	password := c.PostForm("password")
	sourceId := c.PostForm("sourceId")

	//双因子认证
	key := c.PostForm("is_key")

	var err error

	if !commons.StringIsEmpty(password, 5) {
		appG.Response(http.StatusOK, e.ParamsError, username)
		return
	}

	user := users.User{}
	user.UserName = username
	user.Password = password
	sourceId, uid, flag, code, m := user.Login(sourceId, key)

	//	//	//authService := auth.User{}
	//	//	//authService.Uid = uid

	//source, uTime, flag := authService.Check()
	//code := e.SUCCESS
	if !flag {
		appG.Response(http.StatusOK, code, nil)
		return
	}
	//token, _ := redis.GetValue(commons.StringJoin([]string{settings.PrefixToken, uid, "_", sourceId}), settings.RDb1)
	//if token == "" {
	//	token, err = jwt.GenerateToken(uid, username, sourceId)
	//
	//	if err != nil {
	//		appG.Response(http.StatusOK, e.UserNotExist, nil)
	//		return
	//	}
	//	redis.SetStringValue(commons.StringJoin([]string{settings.PrefixToken, uid, "_", sourceId}), token, 60*60*3, settings.RDb1)
	//	/**
	//	用户登录设置token
	//	*/
	//}
	// 如果有两个人同时登陆 a账户。后面的人会更新前一个人登陆的token，意味着第一个登陆的人token不可用
	token, err := jwt.GenerateToken(uid, username, sourceId)
	if err != nil {
		appG.Response(http.StatusOK, e.UserNotExist, nil)
		return
	}
	redis.SetStringValue(commons.StringJoin([]string{settings.PrefixToken, uid, "_", sourceId}), token, 60*60*3, settings.RDb1)

	//这里存在问题 TODO
	/**
	解决第三方用户不会给web界面访问的url权限
	*/
	data := map[string]interface{}{"per": m, "token": token}

	if !commons.CheckErr(err) {
		code = e.Error
	}
	appG.Response(http.StatusOK, code, data)

}

/**
用户注册
	UserName:
	Password
	SourceId

	sourceId:后台生成发送给客户
*/
func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	sourceId := c.PostForm("sourceId")
	var appG = app.Gin{C: c}
	/**
	先验证来源是否合法
	*/
	source := users.Source{}
	source.SourceId = sourceId
	if !source.CheckSource() {
		appG.Response(http.StatusOK, e.RegisterError, "")
		return
	}
	user := users.User{}
	user.UserName = username
	user.Password = password
	code := e.Success
	b := user.Register(sourceId)
	if b != e.Success {
		code = int(b)

	}
	appG.Response(http.StatusOK, code, username)
}

func FindUsers(c *gin.Context) {
	size := c.Query("size")
	pageNum := 0
	toInt, b := commons.StrToInt(size)
	if b {
		pageNum = toInt
	}

	user := users.User{}
	us, count := user.FindUsers(pageNum)
	var appG = app.Gin{C: c}
	code := e.Success

	if us == nil {
		code = e.EmptyError
	}

	appG.ResponseNum(http.StatusOK, code, int(count), us)
}

func UpdatePassword(c *gin.Context) {
	uid := c.PostForm("uid")
	password := c.PostForm("password")
	phone := c.PostForm("phone")
	status := c.PostForm("status")
	var appG = app.Gin{C: c}

	user := users.User{}
	user.Uid = uid
	user.Password = password
	user.Phone = phone
	toInt, b := commons.StrToInt(status)
	if !b {
		appG.Response(http.StatusOK, e.ParamsError, uid)
		return
	}
	user.Status = toInt
	code := e.Success
	if user.UpdatePassword() <= settings.Zero {
		code = e.UpdateError
	}
	appG.Response(http.StatusOK, code, uid)
}

//用户批量注册接口
func ARegister(c *gin.Context) {
	appG := app.Gin{C: c}

	file, _, err := c.Request.FormFile("file")
	if !commons.CheckErr(err) {
		appG.Response(http.StatusOK, e.ParamsError, nil)
		return
	}

	user := users.User{}
	b := user.ImportUser(file)
	if !b {
		appG.Response(http.StatusOK, e.ImportError, nil)
		return
	}

	appG.Response(http.StatusOK, e.Success, nil)
}

func SetAdmin(c *gin.Context) {
	appG := app.Gin{C: c}
	uid := c.PostForm("uid")
	staff := c.PostForm("staff")

	toInt, b := commons.StrToInt(staff)
	//toInt只能是1或者-1
	if !b || commons.Abs(int64(toInt)) != 1 || !commons.StringFilter(uid, 32) {
		appG.Response(http.StatusOK, e.ParamsError, uid)
		return
	}

	user := users.User{}
	user.Uid = uid
	user.Staff = toInt
	code := e.Success
	if user.SetAdmin() <= settings.Zero {
		code = e.UpdateError
	}

	appG.Response(http.StatusOK, code, uid)

}
